package com.ruoyi.rcpt.kjry.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.infoRunTaskData.domain.InfoRunTaskData;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import com.ruoyi.rcpt.kjry.domain.Kjry;
import com.ruoyi.rcpt.kjry.domain.KjryFj;
import com.ruoyi.rcpt.kjry.service.IKjryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 科技人员Controller
 *
 * @author huacai
 * @date 2024-05-10
 */
@RestController
@RequestMapping("/kjry/kjry")
public class KjryController extends BaseController {
    @Resource
    private IKjryService kjryService;

    @Resource
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    /**
     * 查询科技人员列表
     */
    @PreAuthorize("@ss.hasPermi('kjry:kjry:list')")
    @GetMapping("/list")
    public TableDataInfo list(Kjry kjry) {
        startPage();
        List<Kjry> list = kjryService.selectKjryList(kjry);
        return getDataTable(list);
    }

    /**
     * 用于选择科技人员 查询科技人员列表
     */
    @PreAuthorize("@ss.hasPermi('kjry:kjry:list')")
    @GetMapping("/selectKjRyLabel")
    public TableDataInfo selectKjRyLabel(Kjry kjry) {
        startPage();
        List<Kjry> list = kjryService.selectKjRyLabel(kjry);
        return getDataTable(list);
    }

    /**
     * 获取已选择科技人员
     */
    @PreAuthorize("@ss.hasPermi('kjry:kjry:list')")
    @GetMapping("/getKjRyLabelByIds/{kjryIds}")
    public AjaxResult getKjRyLabelByIds(@PathVariable String[] kjryIds) {
        List<Kjry> list = kjryService.getKjRyLabelByIds(kjryIds);
        return AjaxResult.success(list);
    }

    /**
     * 导出科技人员列表
     */
    @PreAuthorize("@ss.hasPermi('kjry:kjry:export')")
    @Log(title = "科技人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Kjry kjry) {
        List<Kjry> list = kjryService.selectKjryList(kjry);
        ExcelUtil<Kjry> util = new ExcelUtil<Kjry>(Kjry.class);
        util.exportExcel(response, list, "科技人员数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Kjry> util = new ExcelUtil<Kjry>(Kjry.class);
        util.importTemplateExcel(response, "科技人员数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "科技人员", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('kjry:kjry:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Kjry> util = new ExcelUtil<Kjry>(Kjry.class);
        InputStream inputStream = file.getInputStream();
        List<Kjry> list = util.importExcel(inputStream);
        inputStream.close();
        int count = kjryService.batchInsertKjry(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取科技人员详细信息
     */
    @PreAuthorize("@ss.hasPermi('kjry:kjry:query')")
    @GetMapping(value = "/{kjryId}")
    public AjaxResult getInfo(@PathVariable("kjryId") String kjryId) {
        return success(kjryService.selectKjryByKjryId(kjryId));
    }

    /**
     * 新增科技人员
     */
    @PreAuthorize("@ss.hasPermi('kjry:kjry:add')")
    @Log(title = "科技人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Kjry kjry) {
        kjry.setUserId(Math.toIntExact(getUserId()));
        int i = kjryService.insertKjry(kjry);
        //向流程业务表中加入数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(kjry.getKjryId());
        infoRunTaskData.setBusinessType(3); //业务模块: 人才平台-人员申请
        infoRunTaskData.setBusinessTitle(kjry.getName()); //任务名称: 科技人员姓名
        infoRunTaskData.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());//申请人

        iInfoRunTaskDataService.insertInfoRunTaskData(infoRunTaskData);

        return toAjax(i);
    }

    /**
     * 修改科技人员
     */
    @PreAuthorize("@ss.hasPermi('kjry:kjry:edit')")
    @Log(title = "科技人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Kjry kjry) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessTitle(kjry.getName()); //任务名称: 科技人员姓名
        infoRunTaskData.setBusinessKey(kjry.getKjryId());
        infoRunTaskData.setBusinessType(3); //业务模块: 人才平台-人员申请
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData); //注意updateInfoRunTaskDatas

        return toAjax(kjryService.updateKjry(kjry));
    }

    /**
     * 删除科技人员
     */
    @PreAuthorize("@ss.hasPermi('kjry:kjry:remove')")
    @Log(title = "科技人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{kjryIds}")
    public AjaxResult remove(@PathVariable String[] kjryIds) {
        //同时删除业务关系数据
        for (String kjryId : kjryIds) {
            Kjry kjry = kjryService.selectKjryByKjryId(kjryId);
            iInfoRunTaskDataService.deleteData(kjry.getKjryId(), 3);//业务模块: 人才平台-人员申请
        }
        return toAjax(kjryService.deleteKjryByKjryIds(kjryIds));
    }

    /**
     * 新增科技人员附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertKjryFjByxmsbId(@RequestBody KjryFj kjryFj) {
        return toAjax(kjryService.insertKjryFjByxmsbId(kjryFj));
    }

    /**
     * 根据主表ID查询科技人员附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectKjryFjList(String sId) {
        KjryFj kjryFj = new KjryFj();
        kjryFj.setKjryId(sId);
        return AjaxResult.success(kjryService.selectKjryFjList(kjryFj));
    }

    /**
     * 查询科技人员与工作履历
     */
    @PreAuthorize("@ss.hasPermi('kjry:kjry:query')")
    @GetMapping(value = "/gzlv/{kjryId}")
    public AjaxResult selectKjryGzlvByKjryId(@PathVariable("kjryId") String kjryId) {
        return success(kjryService.selectKjryGzlvByKjryId(kjryId));
    }

    /**
     * 删除科技人员附件
     */
    @Log(title = "科技人员附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult remove(@PathVariable Integer[] fjIds)
    {
        return toAjax(kjryService.deleteRcptKjryFjByFjIds(fjIds));
    }

}
